package com.study.service;

import com.study.utils.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.hibernate.validator.constraints.Length;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotNull;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @author zhuzi
 * 2023/8/11 14:12
 */
@Slf4j
@Validated
@Service
public class UserInfoService {

    /**
     * 方法非Bean类型的入参校验
     * 1 方法参数前加注解
     * 2 执行入参校验
     *
     * @param name 名字
     * @return {@link String}
     */
    public String getByName(@NotNull String name) {
        //执行入参校验
        StackTraceElement st = Thread.currentThread().getStackTrace()[1];
        String methodName = st.getMethodName();
        Method method = null;
        try {
            method = this.getClass().getDeclaredMethod(methodName, String.class);
        } catch (NoSuchMethodException e) {
        }
        // 打印结果
        List<String> strings = ValidationUtil.validNotBean(this, method, new Object[]{name});
        System.out.println(strings);
        return "";
    }

    /**
     * 测试非Bean
     *
     * @param name 名字
     * @return {@link String}
     */
    public String testValid(@Length(min = 8) String name) {
        log.info("参数是[{}]", name);
        return RandomStringUtils.randomAlphabetic(7);
    }
}
